#!/bin/bash

# Database health check script
set -e

DB_HOST=${DB_HOST:-127.0.0.1}

echo "Starting database health check..."

# Check openGauss
echo "Checking openGauss..."
if docker ps | grep -q opengauss; then
    # gsql
    if docker exec opengauss gsql -d postgres -U gaussdb -c "SELECT version();" > /dev/null 2>&1; then
        echo "Bingo! openGauss OK (via gsql)"
    else
        # psql
        if command -v psql > /dev/null 2>&1; then
            if PGPASSWORD=OpenGauss@123 psql -h $DB_HOST -p 5432 -U gaussdb -d postgres -c "SELECT version();" > /dev/null 2>&1; then
                echo "Bingo! openGauss OK (via psql)"
            else
                echo "openGauss connection failed"
            fi
        else
            echo "psql client not found, cannot test openGauss connection"
        fi
    fi
else
    echo "openGauss container not running"
fi

# Check OceanBase
echo "Checking OceanBase..."
if docker ps | grep -q oceanbase-test; then
    # via container obclient
    if docker exec oceanbase-test bash -c "obclient -h127.0.0.1 -P2881 -uroot@sys -pOceanBase@123 -e 'SELECT 1;' > /dev/null 2>&1"; then
        echo "Bingo! OceanBase OK (via container obclient)"
    else
        # via host obclient
        if command -v obclient > /dev/null 2>&1; then
            if obclient -h$DB_HOST -P2881 -uroot@sys -pOceanBase@123 -e "SELECT 1;" > /dev/null 2>&1; then
                echo "Bingo! OceanBase OK (via host obclient)"
            else
                echo "OceanBase connection failed"
            fi
        else
            # via MySQL client
            if command -v mysql > /dev/null 2>&1; then
                if mysql -h$DB_HOST -P2881 -uroot@sys -pOceanBase@123 -e "SELECT 1;" > /dev/null 2>&1; then
                    echo "Bingo! OceanBase OK (via MySQL client)"
                else
                    echo "OceanBase connection failed with MySQL client"
                fi
            else
                echo "No suitable client found for OceanBase check"
            fi
        fi
    fi
else
    echo "OceanBase container not running"
fi

# Check TDengine
echo "Checking TDengine..."
if docker ps | grep -q tdengine-test; then
    if docker exec tdengine-test taos -s "show databases;" > /dev/null 2>&1; then
        echo "Bingo! TDengine OK"
    else
        echo "TDengine connection failed"
    fi
else
    echo "TDengine container not running"
fi

# Check MatrixOne
echo "Checking MatrixOne..."
if docker ps | grep -q matrixone-test; then
    if command -v mysql > /dev/null 2>&1; then
        if mysql -h $DB_HOST -P 6001 -u root -p111 -e "SELECT VERSION();" > /dev/null 2>&1; then
            echo "Bingo! MatrixOne OK"
        else
            echo "MatrixOne connection failed"
        fi
    else
        echo "mysql client not found, cannot test MatrixOne connection"
    fi
else
    echo "MatrixOne container not running"
fi

# Check Milvus
echo "Checking Milvus..."
if docker ps | grep -q milvus-test; then
    if curl -s --max-time 10 "http://$DB_HOST:9091/healthz" > /dev/null 2>&1; then
        echo "Great! Milvus OK"
    else
        echo "Oops! Milvus health endpoint unreachable"
    fi
else
    echo "Oops! Milvus container not running"
fi

echo "All database health checks completed!"